//
//  ServiceActiveView.m
//  GAMCAfterSales
//
//  Created by BigAmazing on 2018/7/18.
//  Copyright © 2018年 glendale. All rights reserved.
//

#import "ServiceActiveView.h"
#import "UIView+MJExtension.h"
#import "ServiceActiveTableViewCell.h"//服务活动
#import "ActiveRepairTableViewCell.h"//维修项目
#import "ActivePartTableViewCell.h"//零件
#import "HelpClass.h"
//RGB颜色
#define RGBColor(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@interface ServiceActiveView()<UITableViewDelegate,UITableViewDataSource,ServiceActiveTableViewCellDelegate>
/**
 服务活动列表
 */
@property(nonatomic,strong)UITableView * serviceActiveTableView;
@property(nonatomic,strong)NSMutableArray *serviceActiveArr;
/**
 维修项目列表
 */
@property(nonatomic,strong)UITableView * repairTableView;
@property(nonatomic,strong)NSMutableArray *repairArr;
@property(nonatomic,strong)NSMutableArray * selectedRepairArr;
/**
 零件列表
 */
@property(nonatomic,strong)UITableView * partTableView;
@property(nonatomic,strong)NSMutableArray *partArr;
@property(nonatomic,strong)NSMutableArray * selectedPartArr;


/**
原因
*/
@property(nonatomic,strong)NSMutableArray * reasonArr;
@property(nonatomic,strong)NSMutableArray * reasonCodeArr;
@property(nonatomic,strong)NSMutableArray * reasonNameArr;
@end

@implementation ServiceActiveView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self==[super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    self.reasonArr = [self readershuDICimform][@"ACTIVITY_REFUSE_REASON_CODE_LIST"];
    if (self.reasonArr.count>0) {
        for (NSDictionary * dic in self.reasonArr) {
            [self.reasonNameArr addObject:dic[@"NAME"]];
            [self.reasonCodeArr addObject:dic[@"CODE"]];
        }
    }
    
    
    
  
    //设置视图基本信息
    [self setupBaseInfo];
    //设置标题
    [self setupTitle];
    
    //设置服务活动选择列表
    [self setupServiceAcitiveListInfo];
    
    //设置维修项目信息列表
    [self setUpRepairListInfo];
    //设置维修配件信息列表
    [self setUpPartListInfo];
    //设置确定按钮
    [self setupMakeSureBtn];
}
-(void)setupMakeSureBtn{
    UIButton * makeSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    makeSureBtn.frame = CGRectMake(400, self.mj_h-60, 100, 45);
    [makeSureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [makeSureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [makeSureBtn setBackgroundImage:[UIImage imageNamed:@"ht05-1.png"] forState:UIControlStateNormal];
    [makeSureBtn addTarget:self action:@selector(makeSureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:makeSureBtn];
  
}
-(void)makeSureBtnClick{
//    三件事  1.校验不参加原因 其他原因    2.将参加服务活动的零件 维修项目加入工单  3.将不参加原因加入工单
//    1.
    MainMenuDataModel * modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    for (NSDictionary * dic in modelIns.activeReasonArr) {
        //没选择不参加原因
        if ([dic[@"MAIN_REASON_CODE"] isEqualToString:@""] ||dic[@"MAIN_REASON_CODE"] == nil) {
            UIAlertView * aler =[[UIAlertView alloc]initWithTitle:nil message:@"请选择不参加服务活动原因" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil
            ];
            [aler show];
            return;
        }
        
        //  选择了其他原因  没在其他原因输入框输入值
        if ([dic[@"MAIN_REASON_CODE"] isEqualToString:@"90501005"] &&[dic[@"OTHER_REASON"] isEqualToString:@""]) {
            UIAlertView * aler =[[UIAlertView alloc]initWithTitle:nil message:@"请填写不参加服务活动的其他原因" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aler show];
            return;
        }
    }
//    2.
    
    for (NSMutableDictionary * dic in self.selectedRepairArr) {
        dic[@"STATUS"] = @"A";
        [modelIns.repairProjects addObject:dic];
    }
    for (NSMutableDictionary * dic in self.selectedPartArr) {
        dic[@"STATUS"] = @"A";
        [modelIns.partList addObject:dic];
    }
    
//    3.remove掉服务活动页面 将不参加原因关联到工单
    if (self.delegate && [self.delegate respondsToSelector:@selector(ServiceActiveViewClickMakeSureBtn)]) {
        [self.delegate ServiceActiveViewClickMakeSureBtn];
    }
}
-(void)setUpPartListInfo{
    //    label
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(_repairTableView.frame)+10, 900-30, 20)];
    titleLabel.text = @"维修配件信息:";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textAlignment =  NSTextAlignmentLeft;
    [self addSubview:titleLabel];
    
    //    table Head
    CGFloat y = 24;
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabel.frame)+5, 660, y)];
    [self addSubview:headerView];
    UILabel * chooseLbl = [self createLabelWithView:headerView frame:CGRectMake(0, 0, 60, y) text:@"序号"];
    [headerView addSubview:chooseLbl];
    
    UILabel * serviceCode = [self createLabelWithView:headerView frame:CGRectMake(60, 0, 300, y) text:@"零件代码"];
    [headerView addSubview:serviceCode];
    
    UILabel * serviceName = [self createLabelWithView:headerView frame:CGRectMake(360, 0, 300, y) text:@"零件名称"];
    [headerView addSubview:serviceName];
    
    
    //    tableView
    _partTableView = [[UITableView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(headerView.frame), 660, 40*3) style:UITableViewStylePlain];
    _partTableView.delegate = self;
    _partTableView.dataSource = self;
    _partTableView.layer.borderColor = [UIColor grayColor].CGColor;
    _partTableView.layer.borderWidth = 1;
    [self addSubview:_partTableView];
}
-(void)setUpRepairListInfo{
    //    label
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(_serviceActiveTableView.frame)+10, 900-30, 20)];
    titleLabel.text = @"维修项目信息:";
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textAlignment =  NSTextAlignmentLeft;
    [self addSubview:titleLabel];
    
    //    table Head
    CGFloat y = 24;
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(titleLabel.frame)+5, 660, y)];
    [self addSubview:headerView];
    UILabel * chooseLbl = [self createLabelWithView:headerView frame:CGRectMake(0, 0, 60, y) text:@"序号"];
    [headerView addSubview:chooseLbl];
    
    UILabel * serviceCode = [self createLabelWithView:headerView frame:CGRectMake(60, 0, 300, y) text:@"维修项目代码"];
    [headerView addSubview:serviceCode];
    
    UILabel * serviceName = [self createLabelWithView:headerView frame:CGRectMake(360, 0, 300, y) text:@"维修项目名称"];
    [headerView addSubview:serviceName];

    
    //    tableView
    _repairTableView = [[UITableView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(headerView.frame), 660, 40*3) style:UITableViewStylePlain];
    _repairTableView.delegate = self;
    _repairTableView.dataSource = self;
    _repairTableView.layer.borderColor = [UIColor grayColor].CGColor;
    _repairTableView.layer.borderWidth = 1;
    [self addSubview:_repairTableView];
}
-(void)setupServiceAcitiveListInfo{
    //    label
    UILabel * serviceLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 42, 900-30, 20)];
    serviceLabel.text = @"服务活动选择列表:";
    serviceLabel.font = [UIFont systemFontOfSize:14];
    serviceLabel.textAlignment =  NSTextAlignmentLeft;
    [self addSubview:serviceLabel];

    //    table Head
    CGFloat y = 24;
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(serviceLabel.frame)+5, 900-30*2, y)];
    [self addSubview:headerView];
    
    UILabel * chooseLbl = [self createLabelWithView:headerView frame:CGRectMake(0, 0, 30, y) text:@"选择"];
    [headerView addSubview:chooseLbl];
    
    UILabel * serviceCode = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(chooseLbl.frame), 0, 150, y) text:@"服务活动代码"];
    [headerView addSubview:serviceCode];
    
    UILabel * serviceName = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(serviceCode.frame), 0, 140, y) text:@"服务活动名称"];
    [headerView addSubview:serviceName];
    
    UILabel * serviceType = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(serviceName.frame), 0, 100, y) text:@"活动类型"];
    [headerView addSubview:serviceType];
    
    UILabel * endDate = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(serviceType.frame), 0, 120, y) text:@"截止日期"];
    [headerView addSubview:endDate];
    
    UILabel * reason = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(endDate.frame), 0, 106, y) text:@"不参加原因"];
    [headerView addSubview:reason];
    
    UILabel * otherReason = [self createLabelWithView:headerView frame:CGRectMake( CGRectGetMaxX(reason.frame), 0, 195, y) text:@"其他原因"];
    [headerView addSubview:otherReason];
    
    
    //    tableView
    _serviceActiveTableView = [[UITableView alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(headerView.frame), 900-30*2, 40*3+5) style:UITableViewStylePlain];
    _serviceActiveTableView.delegate = self;
    _serviceActiveTableView.dataSource = self;
    _serviceActiveTableView.layer.borderColor = [UIColor grayColor].CGColor;
    _serviceActiveTableView.layer.borderWidth = 1;
    [self addSubview:_serviceActiveTableView];
    
}

-(void)setupTitle{
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.mj_w, 30)];
    title.text = @"服务活动提醒";
    title.font = [UIFont boldSystemFontOfSize:20.f];
    title.textAlignment =  NSTextAlignmentCenter;
    [self addSubview:title];
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(title.frame)+10, self.mj_w-40, 2)];
    line.backgroundColor = [UIColor redColor];
    [self addSubview:line];
}
-(void)setupBaseInfo{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 1;
}

#pragma mark  tableviewDelegate   --dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //
    if (tableView == _serviceActiveTableView) {
        return self.serviceActiveArr.count;
    }else if (tableView == _repairTableView){
        return self.selectedRepairArr.count;
    }else if (tableView == _partTableView){
        return self.selectedPartArr.count;
    }else{
        return 0;
    }
    

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _serviceActiveTableView) {
        static NSString * Id = @"serviceActive";
        ServiceActiveTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Id];
        if (cell == nil) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"ServiceActiveTableViewCell" owner:self options:nil].firstObject;
            cell.serviceDic = self.serviceActiveArr[indexPath.row];
            cell.reasonNameArr = self.reasonNameArr;
            cell.reasonCodeArr = self.reasonCodeArr;
            cell.vin = self.vin;
            cell.delegate = self;
        }
        return cell;
    }else if (tableView == _repairTableView){
        static NSString * Id = @"repair";
        ActiveRepairTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Id];
        if (cell == nil) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"ActiveRepairTableViewCell" owner:self options:nil].firstObject;
            cell.cellDic = self.selectedRepairArr[indexPath.row];
            cell.numberStr = [NSString stringWithFormat:@"%ld",indexPath.row+1];

        }
        return cell;

    }else if (tableView == _partTableView){
        static NSString * Id = @"part";
        ActivePartTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Id];
        if (cell == nil) {
            cell =  [[NSBundle mainBundle]loadNibNamed:@"ActivePartTableViewCell" owner:self options:nil].firstObject;
            cell.cellDic = self.selectedPartArr[indexPath.row];
            cell.numberStr = [NSString stringWithFormat:@"%d",indexPath.row+1];
        }
        return cell;
    }
   
    return nil;

    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}



// 获取cell
- (id)getCellFromXib:(NSString *)cellXibName
{
    id tableViewCell = nil;
    NSArray *viewArray = [[NSBundle mainBundle] loadNibNamed:cellXibName owner:nil options:nil];
    for (id subView in viewArray)
    {
        if ([subView isKindOfClass:[UITableViewCell class]]) {
            tableViewCell = subView;
            break;
        }
    }
    return tableViewCell;
}

-(UILabel *)createLabelWithView:(UIView *)view frame:(CGRect)frame text:(NSString *)text{
    UILabel *lbl = [[UILabel alloc]initWithFrame:frame];
    [view addSubview:lbl];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.text = text;
    lbl.font = [UIFont systemFontOfSize:14];
    lbl.backgroundColor = RGBColor(202,202,202);
  
    return lbl;
}

#pragma mark  --- 服务活动列表选择按钮代理
-(void)chooseSelectedBtn:(BOOL)isSelected ActivityCode:(NSString *)activityCode{
    if (isSelected) {
        
        for (NSMutableDictionary *dic  in self.repairArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:activityCode]) {
                dic[@"SELECTEDCODE"] = @"1";
            }
        }
        
        for (NSMutableDictionary *dic  in self.partArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:activityCode]) {
                dic[@"SELECTEDCODE"] = @"1";
            }
        }
        
    }else{

        for (NSMutableDictionary *dic  in self.repairArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:activityCode]) {
                dic[@"SELECTEDCODE"] = @"0";
            }
        }
        
        for (NSMutableDictionary *dic  in self.partArr) {
            if ([dic[@"ACTIVITY_CODE"] isEqualToString:activityCode]) {
                dic[@"SELECTEDCODE"] = @"0";
            }
        }

    }
    NSMutableArray * tmpRepair = [[NSMutableArray alloc]init];
    NSMutableArray * tmppart = [[NSMutableArray alloc]init];

    for (NSDictionary * dic in self.repairArr) {
        if ([dic[@"SELECTEDCODE"] isEqualToString:@"1"]) {
            [tmpRepair addObject:dic];
        }
    }
  
    for (NSDictionary * dic in self.partArr) {
        if ([dic[@"SELECTEDCODE"] isEqualToString:@"1"]) {
            [tmppart addObject:dic];
        }
    }
    
    self.selectedRepairArr = tmpRepair;
    self.selectedPartArr = tmppart;
    [self.partTableView reloadData];
    [self.repairTableView reloadData];
}
#pragma mark  --- setter--getter
-(NSMutableArray *)serviceActiveArr{
    if (_serviceActiveArr == nil) {
        _serviceActiveArr = [[NSMutableArray alloc]init];
        
    }
    return _serviceActiveArr;
}
-(NSMutableArray *)repairArr{
    if (_repairArr == nil) {
        _repairArr = [[NSMutableArray alloc]init];
    }
    return _repairArr;
}
-(NSMutableArray *)selectedRepairArr{
    if (_selectedRepairArr == nil) {
        _selectedRepairArr = [[NSMutableArray alloc]init];
    }
    return _selectedRepairArr;
}
-(NSMutableArray*)partArr{
    if (_partArr == nil) {
        _partArr = [[NSMutableArray alloc]init];
    }
    return _partArr;
}
-(NSMutableArray*)selectedPartArr{
    if (_selectedPartArr == nil) {
        _selectedPartArr = [[NSMutableArray alloc]init];
    }
    return _selectedPartArr;
}

-(NSMutableArray *)reasonArr{
    if (_reasonArr ==nil) {
        _reasonArr = [[NSMutableArray alloc]init];
    }
    return _reasonArr;
}
-(NSMutableArray *)reasonCodeArr{
    if (_reasonCodeArr == nil) {
        _reasonCodeArr = [[NSMutableArray alloc]init];
    }
    return _reasonCodeArr;
}
-(NSMutableArray *)reasonNameArr{
    if (_reasonNameArr == nil) {
        _reasonNameArr = [[NSMutableArray alloc]init];
    }
    return _reasonNameArr;
}
-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.serviceActiveArr = self.dataDic[@"TT_ACTIVITY"];
    
    self.repairArr = self.dataDic[@"TT_ACTIVITY_LABOUR"];
    self.selectedRepairArr =self.dataDic[@"TT_ACTIVITY_LABOUR"];
    self.partArr = self.dataDic[@"TT_ACTIVITY_PART"];
    self.selectedPartArr =self.dataDic[@"TT_ACTIVITY_PART"];
    [_partTableView reloadData];
    [_serviceActiveTableView reloadData];
    [_repairTableView reloadData];
}

-(NSMutableDictionary *)readershuDICimform
{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *str=[path stringByAppendingPathComponent:@"Dic.text"];
    dic=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    return dic;
}
@end
